package club.itstudio.itouc.config

import org.apache.http.impl.client.HttpClientBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory
import org.springframework.http.converter.StringHttpMessageConverter
import org.springframework.web.client.RestTemplate
import java.nio.charset.StandardCharsets

@Configuration
class RestTemplateConfig {
    @Bean
    fun restTemplate(): RestTemplate {
        val httpClient = HttpClientBuilder.create().
                disableCookieManagement().
                disableRedirectHandling().build()
        val httpRequestFactory = HttpComponentsClientHttpRequestFactory(httpClient)
        httpRequestFactory.setConnectTimeout(10 * 1000)
        val restTemplate = RestTemplate(httpRequestFactory)
        val converter = StringHttpMessageConverter(StandardCharsets.UTF_8)
        restTemplate.messageConverters.add(0, converter)
        return restTemplate
    }
}